<template>
	<view>
		<u-sticky>
			<view class="ffffff mainpadding_top">
				<scroll-view scroll-x="true" style="width: calc(100vw - 30rpx);">
					<view class="flexleft">
						<view class="flexleft hei_text main_size bold" v-for="(item,index) in topList" :key="index">
							<text class="margin_right1" style="white-space: nowrap;"
								@click="back(item,index)">{{item.name}}</text>
							<view class="arrowright1" v-show="index!=topList.length-1"></view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-sticky>
		<view class="mainpadding">
			<!-- <view class="bghuang radius mainpadding flexcenter hei_text bold main_size">第一頁</view> -->
			<view class="margin_top flexleft">
				<!-- <view class="itemlefttab borderhui huang_text main_size bold">代號</view> -->
				<view class="itemcentertab borderhuang hei_text main_size bold">分類</view>
				<!-- <view class="itemrighttab borderhui huang_text main_size bold">產品</view> -->
			</view>
			<view class="margin_top flexleft" style="position: relative;" v-for="item in list.data" :key="item.id"
				@click="classLableClick(item)">
				<!-- <view class="itemlefttab borderhui hei_text main_size bold">{{item.codename}}</view> -->
				<view class="itemcentertab borderhui hei_text main_size bold">{{item.name}}</view>
				<!-- <view class="itemrighttab borderhui hei_text main_size bold">{{item.productname}}</view> -->
				<view class="tiebianarrow flexcenter" v-if="item.is_haschild">
					<view class="arrowright2"></view>
				</view>
			</view>
		</view>
		<!-- 商品列表 -->
		<view class="paddinglr">
			<view class="flexbetween flex_wrap" style="padding-bottom: 150rpx;">
				<view class="goodsBox margin_top ffffff padding_bottom word_wrap" v-for="item in goodsList.data" :key="item.id"
					@click="goGoodsInfo(item.id)">
					<view class="goodsImg">
						<image class="" :src="item.logoimage_text" mode="" v-if="item.logoimage_text"></image>
						<image class="goodsImg" src="../static/images/index/noImg.jpg" mode="" v-else>
					</view>
					<view class="text_j color_san margin_top paddinglr15">{{item.name}}</view>
					<view class="flexbetween margin_top paddinglr15">
						<view class="text_pee color_six">月售{{item.count}}</view>
						<view class="text_pee color_six" v-if="item.end_num!=-1">庫存{{item.end_num}}</view>
						<view class="text_pee color_six" v-if="item.end_num==-1">不限庫存</view>
					</view>
					<view class="flexbetween margin_top paddinglr15">
						<view class="price">$ <text style="font-size: 30rpx;">{{item.price || 0}}</text> </view>
						<image style="width: 34rpx;height: 34rpx;" src="/static/images/index/add_red.png" mode="">
						</image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				lastTapDiffTime: 0,
				lastTapTimeoutFunc: null,
				shop_id: uni.getStorageSync("shop_id"), //当前店鋪id
				pid: 0, //当前分類id
				list: {
					data: []
				}, //分類列表
				topList: [], //頂部展示列表
				topListLength: 0,
				shopclassify_id: "",
				page: 1,
				limit: 12,
				goodsList: {
					data: []
				}
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg))
			console.log(data);
			this.topList = data
			this.pid = this.topList[this.topList.length - 1].id
			this.topListLength = this.topList.length
			this.getCateList()
		},
		methods: {
			async getCateList() { //分類列表
				let _this = this
				let res = await this.http.request('/api/shop/shopclass', 'POST', {
					shop_id: this.shop_id, //店鋪id
					pid: this.pid
				})
				if (res.code == 1) {
					this.list.data = res.data
					this.getGoodsList()
				} else if (res.code == 0) {
					// this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async getGoodsList(isPage, page) { //商品列表
				let _this = this
				uni.showLoading({
					mask: true,
					title: "請稍後"
				})
				let res = await this.http.request('/api/shop/listGoods', 'POST', {
					page: page || 1,
					limit: _this.limit,
					shop_id: this.shop_id,
					shopclassify_id: this.pid,
					type: this.type, //排序類型默認0，1=銷量降，2-=銷量升，3=價格降 4=價格升
				})
				uni.hideLoading()
				if (res.code == 1) {
					let resList = res.data;
					let dataList = _this.goodsList;
					if (isPage == true) {
						_this.goodsList.data = dataList.data.concat(resList.data);
					} else {
						this.goodsList = res.data
						_this.goodsList.data = resList.data;
					}
				}
			},
			onReachBottom() {
				let _this = this;
				let list = _this.goodsList;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.getGoodsList(true, page);
			},
			classLableClick(item) { // 單击或双击
				const _this = this;
				// 当前時間
				const curTime = new Date().getTime();
				// 上次點擊時間
				const lastTime = _this.lastTapDiffTime;
				// 更新上次點擊時間
				_this.lastTapDiffTime = curTime;
				// 两次點擊间隔小於250毫秒, 認爲是双击
				const diff = curTime - lastTime;
				if (diff < 250) {
					// console.log('双击');
					// 执行方法
					// _this.xxxxx()
					// 成功触發双击事件时，取消單击事件的执行
					clearTimeout(_this.lastTapTimeoutFunc);
				} else {
					// 單击事件延时250毫秒执行
					_this.lastTapTimeoutFunc = setTimeout(function() {
						// console.log('單击');
						if ((item.pid == 0 && item.is_haschild == 0) || (item.pid != 0 && item.is_haschild ==
								0)) {
							_this.pid = item.id
							_this.list.data = []
							_this.getGoodsList()
						} else {
							_this.pid = item.id
							_this.getGoodsList()
							_this.getCateList()
						}
						_this.topList.push(item)
						// console.log(_this.topList)
						// console.log(_this.pid)
						// 执行方法
						// _this.xxxxx()
					}, 250);
				}
			},
			back(item, index) { //點擊頂部每一项返回上一分類
				if (index < this.topListLength - 1) {
					return false
				}
				this.pid = item.id
				if (this.pid == 0) {
					this.topList = [{
						name: "首頁",
						id: 0
					}]
					this.getCateList()
				} else {
					this.topList.splice(index + 1, 1)
					this.getCateList()
				}
			},
			goGoodsInfo(id) { //商品詳情
				uni.navigateTo({
					url: "/pages_index/goodsInfoSecond?id=" + id
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	// 商品列表
	.goodsBox {
		width: 330rpx;
		border-radius: 11rpx 11rpx 0 0;

		.goodsImg {
			width: 330rpx;
			height: 330rpx;
			border-radius: 11rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.price {
			font-size: 24rpx;
			font-family: DIN Next LT Pro-Medium, DIN Next LT Pro;
			font-weight: 500;
			color: #FF0000;
		}
	}

	.mainpadding {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.mainpadding_top {
		padding: 30rpx;
		margin-top: 30rpx;
		box-sizing: border-box;
	}

	.hei_text {
		color: #333;
	}

	.bold {
		font-weight: bold;
	}

	.main_size {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.arrowright1 {
		background-image: url("@/static/images/index/arrowright1.png");
		width: 40rpx;
		height: 40rpx;
		background-size: 100% 100%;
	}

	.arrowright2 {
		background-image: url("@/static/images/index/arrowright2.png");
		width: 20rpx;
		height: 20rpx;
		background-size: 100% 100%;
	}

	.huang_text {
		color: #ffb800;
	}

	.baibtn {
		padding: 15rpx 20rpx;
	}

	.posibottom {
		position: fixed;
		bottom: 30rpx;
		left: 0;
		width: 100%;
		box-sizing: border-box;
	}

	/* 提交按钮 */
	.submitbtn {
		width: 100%;
		background-color: #ffb800;
		border-radius: 50rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.itemlefttab {
		padding: 20rpx 0;
		width: 33.33%;
		text-align: center;
		border-right: none;
		border-radius: 10rpx 0 0 10rpx;
	}

	.itemcentertab {
		padding: 20rpx 0;
		width: 98%;
		text-align: center;
	}

	.itemrighttab {
		padding: 20rpx 0;
		width: 48%;
		text-align: center;
		border-left: none;
		border-radius: 0 10rpx 10rpx 0;
	}

	.borderhui {
		border: 1rpx solid rgba(50, 60, 160, .1);
	}
	.borderhuang{
		background-color: #ffb800;
	}

	.tiebianarrow {
		height: 50rpx;
		background-color: #ffb800;
		width: 30rpx;
		position: absolute;
		right: -15rpx;
		top: 50%;
		transform: translate(50%, -50%);
	}
</style>